<template>
  <div id="app">
    <!-- 标题 -->
    <h1>
      <span
        v-for="(item, index) in title"
        :key="index"
        :style="{ animationDelay: ((((0.2 * title.length -1)) / title.length) * index).toFixed(1) + 's', 'animationDuration': (0.2 * title.length).toFixed(1) + 's'}"
        >{{ item }}</span
      >
    </h1>

    <!-- input 输入框 -->
    <div class="input-wrap">
      <input
        class="input"
        type="text"
        v-model="longUrl"
        placeholder="请输入您的网址，如：https://www.baidu.com/"
      />

      <button type="button" class="btn" @click="getLongUrl">生成短链接</button>
    </div>

    <div class="content" v-show="status === 'success'">
      <!-- 生成的短链接 -->
      <div class="short-url">
        <span>短链接：</span>
        <!-- <span class="url">{{ short_url }}</span> -->
        <a class="url" :href="short_url" target="_blank">{{short_url}}</a>

        <!-- <button type="button" class="copy" @click="copy">复制</button> -->
        <a href="short_url"></a>
      </div>

      <!-- 展示生成的二维码 -->
      <div class="qrcode">
        <span>二维码：</span>

        <span class="img" ref="qrcode"></span>
      </div>
    </div>

    <div v-show="status === 'loading'">生成中...</div>

    <div v-show="status === 'err'">{{ message }}</div>
  </div>
</template>

<script>
import QRCode from "QRCode";
const validUrl = require("valid-url");
export default {
  name: "App",
  data() {
    return {
      title: "在线短链接生成工具",
      status: false, // success  loading  err,
      longUrl: "",
      short_url: "",
      message: ""
    };
  },
  methods: {
    /**
     * @methods getLongUrl
     * @description 获取长连接，生成短链接和二维码
     */
    getLongUrl() {
      this.status = true;
      // 清除二维码
      this.$refs.qrcode.innerHTML = '';
      // 1.验证长链接格式是否复合url规范
      // 1.1 验证是否为空
      if (!this.longUrl.replace(/\s*/g, "") && this.status) {
        this.status = false;
        this.message = "长连接不能为空";
      }

      if (!validUrl.isUri(this.longUrl) && this.status) {
        this.status = false;
        this.message = "长连接格式错误";
      }

      if (this.status) {
        // this.status = 'loading';
        this.requestShortUrl(this.longUrl);
      } else {
        this.status = "err";
      }
    },
    /**
     * @methods requestShortUrl
     * @param {String} longUrl 长连接
     * @description 请求短链接
     */
    requestShortUrl(longUrl) {
      let self = this;
      self.status = 'loading'
      fetch("http://127.0.0.1:3000/api/url/shorten", {
        method: "post",
        headers: {
          "Content-Type": "application/json",
          'Accept': "application/json"
        },
        body: JSON.stringify({
          'longUrl': longUrl
        })
      })
        .then(res => res.json())
        .then(res => {
          // console.log(res);
          self.status = 'success';
          self.short_url = res.shortUrl;

          self.creareQRcode()
        })
        .catch(err => {
          self.status = 'err'
          self.message = '短链接生成失败'
          console.log('requestShortUrl方法错误' + err)
        })
    },
    /**
     * @methods creareQRcode
     * @description 生成二维码
     */
    creareQRcode () {
      new QRCode(this.$refs.qrcode, {
        text: this.short_url,
        width: 128,
        height: 128,
        typeNumber: 4,
        colorDark : "#000000",
        colorLight : "#ffffff",
        correctLevel : QRCode.CorrectLevel.H
      })
    }
  }
};
</script>

<style lang="scss">
body,
html {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background-color: #18bc9b;
  overflow: hidden;
}
div {
  margin: 0;
  padding: 0;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-size: 16px;
  text-align: center;
  color: #fff;
}
h1 {
  margin-bottom: 12px;
  font-size: 46px;
  text-align: center;
  letter-spacing: 10px;
  color: #314254;
  font-family: sans-serif;
  span {
    // animation: animate 1.8s linear infinite;
    animation-name: animate;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
  }

  @keyframes animate {
    0%,
    80% {
      color: #314254;
      box-shadow: none;
    }
    100% {
      color: #fff;
      text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 40px #fff, 0 0 80px #fff,
        0 0 100px #fff;
    }
  }
}
.input-wrap {
  width: 60%;
  height: 100px;
  margin: 20vh auto 42px;
  padding: 20px 42px;
  border-radius: 6px;
  box-sizing: border-box;
  background-color: rgba(255, 255, 255, 0.15);
  .input {
    width: calc(80% - 36px);
    height: 100%;
    padding: 0 10px;
    border: none;
    margin-right: 30px;
    border-radius: 6px;
    box-sizing: border-box;
    outline: none;
    background-color: #fff;
  }
  .btn {
    width: 20%;
    height: 100%;
    // padding: 10px 20px;
    box-sizing: border-box;
    border: none;
    border-radius: 6px;
    font-size: 18px;
    color: #fff;
    outline: 0;
    background-color: #314254;
    cursor: pointer;
  }
}

.short-url {
  width: 60%;
  margin: 0 auto;
  padding: 12px 30px;
  box-sizing: border-box;
  font-size: 16px;
  text-align: left;
  color: #fff;
  background-color: #314254;
  .url {
    width: 50%;
    margin-right: 20%;
    text-decoration: none;
    color: #fff;
    display: inline-block;
  }
  .copy {
    width: 67px;
    height: 25px;
    padding: 0;
    border: none;
    border-radius: 3px;
    font-size: 13px;
    text-align: center;
    line-height: 25px;
    background-color: #fff;
  }
}

.qrcode {
  width: 60%;
  margin: 42px auto 0;
  padding: 12px 30px;
  box-sizing: border-box;
  font-size: 16px;
  text-align: left;
  color: #fff;
  .img {
    vertical-align: text-top;
    display: inline-block;
  }
}
</style>
